package ltd.newbee.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import ltd.newbee.mall.api.mall.vo.NewBeeMallIndexCarouselVO;
import ltd.newbee.mall.mapper.CarouselMapper;
import ltd.newbee.mall.entity.Carousel;
import ltd.newbee.mall.service.NewBeeMallCarouselService;
import ltd.newbee.mall.util.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class NewBeeMallCarouselServiceImpl implements NewBeeMallCarouselService {
    @Autowired
    private CarouselMapper carouselMapper;

    @Override
    public List<NewBeeMallIndexCarouselVO> getCarouselsForIndex(int number) {
        List<NewBeeMallIndexCarouselVO> newBeeMallIndexConfigGoodsVOList = new ArrayList<>(number);
        LambdaQueryWrapper<Carousel> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper
                .eq(Carousel::getIsDeleted, 0)
                .orderByDesc(Carousel::getCarouselRank);
        IPage<Carousel> page = new Page<>(1, number);
        IPage<Carousel> carouselListPage = carouselMapper.selectPage(page, queryWrapper);
        List<Carousel> carouselList = carouselListPage.getRecords();
        if (!CollectionUtils.isEmpty(carouselList)) {
            newBeeMallIndexConfigGoodsVOList = BeanUtil.copyList(carouselList, NewBeeMallIndexCarouselVO.class);
        }
        return newBeeMallIndexConfigGoodsVOList;
    }
}
